package com.example.meituan.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

import com.example.meituan.activity.bean.TalkBean;
import com.example.meituan.db.DBUntil;

public class TalkDao {

    public static SQLiteDatabase db= DBUntil.con;


    // 添加对话
    public long addTalk(TalkBean talk) {
        ContentValues values = new ContentValues();
        values.put("s_id", talk.getId());
        values.put("s_user_id", talk.getUserId());
        values.put("s_business_id", talk.getShopId());
        values.put("s_time", talk.getTime());
        values.put("s_talk", talk.getTalk());

        return db.insert("d_talk", null, values);
    }

    // 删除对话
    public int deleteTalkById(String id) {
        return db.delete("d_talk", "s_id=?", new String[]{id});
    }

    // 更新对话
    public int updateTalk(TalkBean talk) {
        ContentValues values = new ContentValues();
        values.put("s_user_id", talk.getUserId());
        values.put("s_shop_id", talk.getShopId());
        values.put("s_time", talk.getTime());
        values.put("s_talk", talk.getTalk());

        return db.update("d_talk", values, "s_id=?", new String[]{talk.getId()});
    }

    // 查询所有对话
    public static List<TalkBean> getAllTalksById(String userId, String businessId) {
        List<TalkBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_talk Where s_user_id=? And s_bussiness_id=?", new String[]{userId,businessId});


        try {
            while (cursor.moveToNext()) {
                TalkBean talk = createTalkFromCursor(cursor);
                list.add(talk);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    public static List<TalkBean> getAllTalksByUserId(String userId) {
        List<TalkBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_talk Where s_user_id=? ", new String[]{userId});

        try {
            while (cursor.moveToNext()) {
                TalkBean talk = createTalkFromCursor(cursor);
                list.add(talk);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    public static List<TalkBean> getAllTalksByBUsinessId(String businessId) {
        List<TalkBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_talk Where s_bussiness_id=? ", new String[]{businessId});

        try {
            while (cursor.moveToNext()) {
                TalkBean talk = createTalkFromCursor(cursor);
                list.add(talk);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    // 根据 ID 查询单个对话
    public TalkBean getTalkById(String id) {
        Cursor cursor = db.rawQuery("select * from d_talk where s_id=?", new String[]{id});

        try {
            if (cursor.moveToNext()) {
                return createTalkFromCursor(cursor);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return null;
    }

    public static TalkBean getTalkByUserIdLeast(String userId) {
        Cursor cursor = db.rawQuery(
                "SELECT * FROM d_talk WHERE s_user_id=? ORDER BY s_time DESC LIMIT 1",
                new String[]{userId}
        );

        try {
            if (cursor.moveToNext()) {
                return createTalkFromCursor(cursor);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return null;
    }

    public TalkBean getTalkByBusinessIdLeast(String businessId) {
        Cursor cursor = db.rawQuery(
                "SELECT * FROM d_talk WHERE s_bussiness_id=? ORDER BY s_time DESC LIMIT 1",
                new String[]{businessId}
        );

        try {
            if (cursor.moveToNext()) {
                return createTalkFromCursor(cursor);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return null;
    }

    // 辅助方法：从游标创建对话对象
    private static TalkBean createTalkFromCursor(Cursor cursor) {
        return new TalkBean(
                cursor.getString(cursor.getColumnIndexOrThrow("s_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_user_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_bussiness_id")),
                cursor.getLong(cursor.getColumnIndexOrThrow("s_time")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_talk")),
                cursor.getLong(cursor.getColumnIndexOrThrow("s_if_userId"))
        );
    }
}
